﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using ZG_member.BLL;

namespace ZG_member
{
    public partial class goodcount : Form
    {
        MemberSaleForm ms;
        int goodshuliang = 0;
        public goodcount(int shuliang,MemberSaleForm mss)
        {
            InitializeComponent();
            ms = mss;
            goodshuliang = shuliang;
            this.Opacity = 0;
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
        }

        private void goodcount_Load(object sender, EventArgs e)
        {
            this.timer1.Start();
            this.dateTimePicker1.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            //try
            //{
                //if (this.textBox1.Text.Trim() == "")
                //{
                //    this.Close();
                //    return;
                //}
                for (int i = 0; i < ms.dataGridView2.Rows.Count; i++)
                {

                    if (ms.dataGridView2.Rows.Count <= 0)
                    {
                        break;
                    }
                    //if (int.Parse(ms.dataGridView1.Rows[ms.dataGridView1.CurrentRow.Index].Cells["goodcount"].Value.ToString()) <= 0 && decimal.Parse(ms.dataGridView1.Rows[ms.dataGridView1.CurrentRow.Index].Cells["goodcount"].Value.ToString()) >= decimal.Parse(this.textBox1.Text.ToString()))
                    //{
                    //    this.Close();
                    //    return;
                    //}
                    //if (ms.dataGridView1.Rows[ms.dataGridView1.CurrentRow.Index].Cells["goodname"].Value.ToString() == ms.dataGridView2.Rows[i].Cells["goodsname"].Value.ToString())
                    //{


                    //    ms.dataGridView2.Rows[i].Cells["goodscount"].Value = decimal.Parse(ms.dataGridView2.Rows[i].Cells["goodscount"].Value.ToString()) + 1;
                    //    ms.dataGridView1.Rows[ms.dataGridView1.CurrentRow.Index].Cells["goodcount"].Value = (decimal.Parse(ms.dataGridView1.Rows[ms.dataGridView1.CurrentRow.Index].Cells["goodcount"].Value.ToString()) - 1).ToString();
                    //    this.Close();
                    //    return;
                    //}
                }
                if (int.Parse(ms.dataGridView1.Rows[ms.dataGridView1.CurrentRow.Index].Cells["goodcount"].Value.ToString()) > 0 && decimal.Parse(ms.dataGridView1.Rows[ms.dataGridView1.CurrentRow.Index].Cells["goodcount"].Value.ToString()) < 1)
                {
                    this.Close();
                    return;
                }
                ms.dataGridView1.Rows[ms.dataGridView1.CurrentRow.Index].Cells["goodcount"].Value = (decimal.Parse(ms.dataGridView1.Rows[ms.dataGridView1.CurrentRow.Index].Cells["goodcount"].Value.ToString()) - 1).ToString();
                string[] tiaojian = { ms.dataGridView1.Rows[ms.dataGridView1.CurrentRow.Index].Cells["goodsid"].Value.ToString(), ms.dataGridView1.Rows[ms.dataGridView1.CurrentRow.Index].Cells["goodname"].Value.ToString(),"1", ms.dataGridView1.Rows[ms.dataGridView1.CurrentRow.Index].Cells["danwei"].Value.ToString(),dateTimePicker1.Text+" "+dateTimePicker2.Text};
                // ms.dataGridView1.Rows[ms.dataGridView1.CurrentRow.Index].Cells["goodcount"].Value = "0";
                this.ms.dataGridView2.Rows.Add(tiaojian);
                this.Close();
            //}
            //catch (Exception)
            //{

            //    throw;
            //}
            //finally
            //{
                //goods goods = new goods();
                //Model.goods goodsMode = new Model.goods();
                //decimal money = 0;
                //for (int i = 0; i < this.ms.dataGridView2.Rows.Count; i++)
                //{
                //    decimal danjia = Convert.ToDecimal(goods.getModelGoodsIdString(this.ms.dataGridView2.Rows[i].Cells["goodid"].Value.ToString()).price);
                //    int shuliang = Convert.ToInt32(this.ms.dataGridView2.Rows[i].Cells["goodscount"].Value);
                //    money += danjia * shuliang;

                //}
               
                //decimal kaneiyue = Convert.ToDecimal(this.ms.TxtBalance.Text);
                //if (kaneiyue < money)
                //{
                //    this.ms.TxtCrashPay.Text = (money - kaneiyue).ToString();
                //    this.ms.TxtCardPay.Text = this.ms.TxtBalance.Text;
                //}
                //else
                //{
                //    this.ms.TxtCardPay.Text = money.ToString();
                //}
                //this.ms.TxtConsume.Text = (Convert.ToDecimal(this.ms.TxtCardPay.Text) + Convert.ToDecimal(this.ms.TxtCrashPay.Text)).ToString();
            //}
          
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //timer开启
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            //timer关闭
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();

            }
        }
        int flag = 0;

        private void goodcount_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                if (e.KeyChar == '.')
                {
                    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                        e.Handled = true;
                }
                else
                e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
